|
|
 |
|
|
|
|
For Y = 0 To hgt - 1
For X = 0 To wid - 1
With pixels(X, Y)
' Get the combined 2 bytes for this pixel. two_bytes = bytes(X * 2, Y) + _
bytes(X * 2 + 1, Y) * 256&
' Separate the pixel's components. .rgbBlue = two_bytes Mod 32
two_bytes = two_bytes \ 32
.rgbGreen = two_bytes Mod 32
two_bytes = two_bytes \ 32
.rgbRed = two_bytes
End With
Next X
Next Y |
|
|
|
|
|
|
|
|
Transferring data for a 24-bit image is easier because each color component is represented by its own byte. In fact, the bytes in a row of the bytes array correspond exactly to the bytes in a row of pixels array. The only annoying detail is the bytes array may contain some extra bytes at the end to make each row have an even number of bytes. If it were not for this fact, you could make GetBitmapBits copy its data directly into the pixels array. |
|
|
|
|
|
|
|
|
Instead, you can use the CopyMemory API function to copy the rows of data directly into the pixels array. Using CopyMemory is much faster than looping through the array copying the bytes one at a time. |
|
|
|
 |
|
|
|
|
For Y = 0 To hgt - 1
CopyMemory pixels(0, Y), bytes(0, Y), wid * 3
Next Y |
|
|
|
|
|
|
|
|
Transferring the pixel values in and out of an RGBTriplet array takes a little longer than working with them directly in the array filled by GetBitmapBits, but it is much simpler, particularly for the stranger color models. Using the original array, you need to search for a pixel's bits and assemble them into red, green, and blue color components in one of several different ways, depending on the image's color depth. Using the array of RGBTriplet values, you can access any pixel's color components quickly and easily. |
|
|
|
|
|
|
|
|
Example program CopyDDB, shown in Figure 3.2, demonstrates this approach. The code is not included here because it is quite long. You can find the complete source code on the CD-ROM. |
|
|
|
|
|
|
|
|
When you click the Copy button, the program copies a source picture into two picture boxes. It draws some colored boxes on the resulting image to show that it can manipulate pixel values using the RGBTriplets. |
|
|
|
|
|
|
|
|
The program uses Point and PSet to copy the image into the first picture box. The program uses an array of RGBTriplet values to copy the image into the second picture box. In one test, Point and PSet took more than 20 times longer than using the RGBTriplet array. |
|
|
|
|
|